// pages/myQes/myQes.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    input: 0, //鼠标聚焦
    keyword:'',

    question: [],
    quesdisplay: [], //显示的问题信息
    start: 0, //渲染开始位置
    skipNum: 10, //追加数据数量
    count: 0,

    //筛选
    myQes: 1,
    myCollect: 0,
  },
  //鼠标聚焦
  focus: function () {
    this.setData({
      input: 1
    })
  },

  //鼠标移开
  blur: function () {
    this.setData({
      input: 0
    })
  },


  //搜索
  input:function(e){
    this.setData({
      keyword:e.detail.value
    })
  },
  search:function(){
    var that = this
     wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url:  'https://www.cerywxr.com/treehole/index.php/Home/ques/search_my_qes',
      data: { 
        keyword: that.data.keyword,
        myqes:that.data.myQes,
        user_id:getApp().globalData.userInfo.user_id,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            
          })
        } else if(res.data.error_code == 1){
          wx.showModal({
            title: '提示!',
            content: '搜索内容不得为空',
            showCancel: false,
            success(res) { }
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀~!',
          content: '网络状态不佳呢！',
          showCancel: false,
          success(res) { }
        })
      },
      complete: function (res) {   //注意！
        wx.hideLoading()
      }
    })
  },

  /**
   * 筛选
   */
  //我的提问
  myQes: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/getMyQes',
      data: {
        user_id: getApp().globalData.userInfo.user_id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            myQes: 1,
            myCollect: 0
          })

        } else {
          console.log('qes页面错误', res.data)
        }
        wx.hideLoading()
      }
    })
  },
  //我的关注
  myCollect: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/getMyFollow',
      data: {
        user_id: getApp().globalData.userInfo.user_id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        
        if (res.data.error_code == 0) {
          if (res.data.count > 100) {
            that.setData({
              question: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              question: res.data.data,
              count: res.data.count,

            })
          }
          that.setData({
            quesdisplay: that.data.question.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            //筛选
            myQes: 0,
            myCollect: 1
          })

        } else {
          console.log('qes页面', res.data)
        }
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.myQes()
  },

  //触底刷新，获取更多
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var end = that.data.start + that.data.skipNum
    var newData = that.data.question.slice(that.data.start, end)
    that.setData({
      quesdisplay: that.data.quesdisplay.concat(newData), //拼接数组
      start: end
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500); //计时器  
  },
})